

capture log close
log using $log/AED_analysis_08_sensitivity_inference, text replace

/* ------------------------------------------------------------------------------------ */
/* --- Sensitivity Analysis Table A.10 ------------------------------------------------- */
/* ------------------------------------------------------------------------------------ */ 


clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off

fdzinstall boottest
fdzinstall acreg
fdzinstall ranktest
fdzinstall hdfe
fdzinstall ivreg2
fdzinstall winsor2

   
#d;
global xvars " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				kinder_* schulabschluss_* ausbildung_*
				iza_last_daily_income berufskat_*
				iza_ende_last iza_vztz_last iza_zeit_la
				iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3
				iza_lnwage_tminus1 iza_lnwage_tminus2 iza_lnwage_tminus3 
				eintritts_jahr_* eintritts_monat_*
				";
				#d cr
				
				
		

	
		use $data/data_est_border, clear
		
	
	
	*Merge coordinates for spatial correlation
	
		
	keep if eintritt<=200805 & eintritt>=200706
	
	
	tab iza_kind, gen(kinder_)
	tab iza_abschluss_kat, gen(schulabschluss_)
	tab iza_ausb_kat, gen(ausbildung_)
	tab iza_beruf_kat, gen(berufskat_)
	tab year, gen (eintritts_jahr_)
	tab entry_m, gen(eintritts_monat_)
	
	
#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	#d;
	global outc " month_sv1_24 
	 earnings1_24	
	 iza_umzug24
	 mobihilfe_24
	 umzugskosten_24";
	#d cr
	
	
	cap drop weight
	gen weight	=1/fweight_reg
	encode region_pair, generate(region_pair2)
	
	foreach var in $outc {
		
		qui reg `var'  log_intensity_alt  $xvars  pair_*  [pw=1/fweight_reg], cluster(region)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se0=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		
		matrix d1=b[1..1,1..1],se
		
		boottest log_intensity_alt, reps(9999) cluster(region) nograph boot(wild)
	    matrix p = r(p)	  
		
		
		qui acreg `var' log_intensity_alt $xvars [pw=weight], pfe1(region_pair2) spatial latitude(Breite) longitude(Länge) dist(25)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		
		
		matrix d2=se
		
		qui acreg `var' log_intensity_alt $xvars [pw=weight], pfe1(region_pair2) spatial latitude(Breite) longitude(Länge) dist(50)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		
		
		matrix d3=se
		
		qui acreg `var' log_intensity_alt $xvars [pw=weight], pfe1(region_pair2) spatial latitude(Breite) longitude(Länge) dist(150)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		
		
		matrix d4=se
		
		
		matrix d_`var' = d1,d2,d3,d4,p
	}
	


matrix d = d_month_sv1_24\d_earnings1_24\d_umzugskosten_24

matrix rownames d = "Months employed" "Earnings"  "Relocation assistance" 

matrix colname d = "Coef" "SE clustered" "SE spatial 25km" "SE spatial 50km"  "SE spatial 150km" "P-value WCB"
mat list d


log close
